﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using BehaviorDesigner.Runtime;
using BehaviorDesigner.Runtime.Tasks.Movement;



public class SkillController : MonoBehaviour
{

    public GameObject pet;
    private float speed = 20;
    private ParticleSystem.Particle[] m_Particles;
    private GameObject m_skill;
    private ParticleSystem particle;

    private float cover;//冰冻系数
    public Material iceDeathMat;//冰冻材质球
    public GameObject WaterNeedle, Kunai_Kunai;
    private List<GameObject> skillList = new List<GameObject>();



    void Start()
    { 
    }

    void Update()
        {

         if (skillList.Count<=0)
            return;

         float step = speed * Time.deltaTime;

        for(int j = 0; j < skillList.Count; j++)
        {
            if (skillList[j] == null)
            {
                skillList.RemoveAt(j);
                j--;
                continue;
            }

            particle = skillList[j].GetComponentInChildren<ParticleSystem>();
            m_Particles = new ParticleSystem.Particle[particle.maxParticles];

            int size = particle.GetParticles(m_Particles);

            for (int i = 0; i < size; i++)
            {
                m_Particles[i].position = Vector3.MoveTowards(m_Particles[i].position,
                pet.transform.position, step);
            }

            particle.SetParticles(m_Particles, size);

        }



        if (Input.GetMouseButtonDown(1))
        {
            //SnowCover();
            StartInvoke();
        }
    }

        
    /// 释放技能

    public void ReleaseSkill(GameObject skill)
    {

        if (gameObject.name.Equals("EnemyBoss_orc"))
        {
            // 小boss
            int num  = Random.RandomRange(0, 3);
            switch (num)
            {
                case 0:

                    m_skill = Instantiate(skill);
                    m_skill.transform.position = transform.position;
                    m_skill.transform.rotation = transform.localRotation;
                    break;
                case 1:
                    m_skill = Instantiate(Kunai_Kunai);
                    m_skill.transform.position = pet.transform.position;
                    
                    m_skill.transform.rotation = pet.transform.rotation;
                    break;
              case 2:
                    m_skill = Instantiate(WaterNeedle);
                    m_skill.transform.position = transform.position;
                    m_skill.transform.rotation = transform.localRotation;
                    break;
            }
        }
        else{
            m_skill = Instantiate(skill);
            //生成位置
            m_skill.transform.position = transform.position;
            //角度
            m_skill.transform.rotation = transform.localRotation;
        }
        skillList.Add(m_skill);
        Destroy(m_skill,5);
      
    }

    public void StartInvoke()
    {
        InvokeRepeating("SnowCover", 0, 0.1f);
    }


    /// <summary>
    /// 小怪冰冻特效
    /// </summary>

    void SnowCover()
    {
        //将原来一个材质球换成两个
        Material[] iceDeathMaterials = new Material[2] { GetComponentInChildren<Renderer>().material, iceDeathMat };

        GetComponentInChildren<Renderer>().materials = iceDeathMaterials;

        Material[] currentMaterial = GetComponentInChildren<Renderer>().materials;

        cover = Mathf.Lerp(cover, 1, Time.deltaTime * 0.1f);

        currentMaterial[1].SetFloat("_Coverage", cover);

        if (cover >= 0.5f)
        {
            CancelInvoke("SnowCover");
            var mm = this.GetComponent<MonsterManager>();
            mm.MonsterDie();

        }
            
    }

}

